package com.bs.bszhsq.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bszhsq.entity.Helpold;
import com.bs.bszhsq.entity.RoleMenu;
import com.bs.bszhsq.mapper.RoleMenuMapper;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
import com.bs.bszhsq.utils.R;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.bs.bszhsq.service.IRoleService;
import com.bs.bszhsq.entity.Role;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 章宇光
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理接口")
        public class RoleController {
    //service注入
@Resource
private IRoleService roleService;
@Resource
private RoleMenuMapper roleMenuMapper;

@PostMapping
@ApiOperation(value = "新增")
public R save(@RequestBody Role role){
        boolean save= roleService.save(role);
        return R.write(save);
        }


@PostMapping("/delete")
@ApiOperation(value = "删除")
public R delete(@RequestBody Long id){
        boolean b=roleService.removeById(id);
        return R.write(b);
        }

@PostMapping("/update")
@ApiOperation(value = "修改")
public R update(@RequestBody Role role) {
        boolean update = roleService.updateById(role);
        return R.write(update);
        }


@PostMapping("/deletebatch")
@ApiOperation(value = "批量删除")
public R deletebatch(@RequestBody List<Long> ids) {
        boolean b = roleService.removeByIds(ids);
        return R.write(b);
        }


@GetMapping("/page")
@ApiOperation(value = "分页查询")
public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam(defaultValue = "") String name) {
        IPage<Role> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Role> qw = new QueryWrapper<>();
        if (!"".equals(name)) {
                qw.like("name", name);
        }
        qw.orderByDesc("id");
        IPage<Role> page1 = roleService.page(page, qw);
        return R.success(page1);
        }
@PostMapping("/roleMenu/{roleId}")
@ApiOperation(value = "角色菜单")
public R roleMenu(@PathVariable Integer roleId,@RequestBody List<Integer> menuId){
        roleService.setRoleMenu(roleId,menuId);
        return R.success();
}
@GetMapping("/roleMenu/{roleId}")
        @ApiOperation(value = "获取角色权限")
        public R getRoleMenu(@PathVariable Integer roleId){
        LambdaQueryWrapper<RoleMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(RoleMenu::getRoleId,roleId);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(lqw);
        List<Integer> menus=new ArrayList<>();
        for (RoleMenu roleMenu : roleMenus) {
                menus.add(roleMenu.getMenuId());
        }
        return R.success(menus);
}

        }
